package com.aoip.telemetry.telemetry.configureui.business.analysis.stgroup.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aoip.telemetry.telemetry.configureui.business.analysis.stgroup.service.StAnalysisGroupService;
import com.aoip.telemetry.telemetry.configureui.common.annotation.Log;
import com.aoip.telemetry.telemetry.configureui.common.core.controller.BaseController;
import com.aoip.telemetry.telemetry.core.entity.AjaxResult;
import com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo;
import com.aoip.telemetry.telemetry.configureui.common.enums.BusinessType;
import com.aoip.telemetry.telemetry.entity.equipment.RelStAnalysisGroupConfigEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 测站所属解析组关系配置
 *
 * @author chenbai
 * @version 1.0
 * @date 2021/12/31 14:55
 */
@RestController
public class StAnalysisGroupController extends BaseController {

    @Resource
    private StAnalysisGroupService service;

    /**
     * 查询测站与解析组关联关系分页列表
     *
     * @param entity 测站与解析组关联关系实体
     * @return com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo
     * @author chenbai
     * @date 2021/12/31
     **/
    @GetMapping("/v1/GET/st/group/page")
    public TableDataInfo page(RelStAnalysisGroupConfigEntity entity) {
        startPage();
        List<RelStAnalysisGroupConfigEntity> list = service.list(entity);
        return getDataTable(list);
    }

    /**
     * 根据主键id查询关联关系信息，返回单条关联关系数据
     *
     * @param id 测站与解析组关联关系主键
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2021/12/31
     **/
    @GetMapping("/v1/GET/st/group/{id}")
    public AjaxResult stGroup(@PathVariable String id) {
        return AjaxResult.success(service.stGroup(id));
    }

    /**
     * 根据测站编码，返回一条或多条解析组数据
     *
     * @param stCd 测站编码
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2022/1/10
     **/
    @GetMapping("/v1/GET/st/groups/{stCd}")
    public AjaxResult analysisGroupListByStCd(@PathVariable String stCd) {
        return AjaxResult.success(service.analysisGroupListByStCd(stCd));
    }

    /**
     * 根据解析组编码，返回解析组挂载的测站列表
     *
     * @param groupId 解析组编码
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2022/1/10
     **/
    @GetMapping("/v1/GET/stCodes/group/{groupId}")
    public AjaxResult stCodesByGroupId(@PathVariable String groupId) {
        return AjaxResult.success(service.stCodesByGroupId(groupId));
    }
    /**
     * 新增测站与解析组关联关系
     *
     * @param params 测站编码, 解析组件编码
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2021/12/31
     **/
    @Log(title = "测站与解析组关联关系", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/stCode/groupIds")
    public AjaxResult stCodeGroupIds(@Validated @RequestBody JSONObject params) {
        String stCode = JSON.parseObject(JSON.toJSONString(params.get("stCode")), String.class);
        List<String> groupIds = JSON.parseArray(JSON.toJSONString(params.get("groupIds")), String.class);
        return toAjax(service.insert(stCode, groupIds));
    }

    /**
     * 新增测站与解析组关联关系
     *
     * @param params 测站编码, 解析组件编码
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2021/12/31
     **/
    @Log(title = "测站与解析组关联关系", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/stCodes/groupId")
    public AjaxResult stCodesGroupId(@Validated @RequestBody JSONObject params) {
        String groupId = JSON.parseObject(JSON.toJSONString(params.get("groupId")), String.class);
        List<String> stCodes = JSON.parseArray(JSON.toJSONString(params.get("stCodes")), String.class);
        return toAjax(service.insert(stCodes, groupId));
    }


    /**
     * 修改测站与解析组关联关系
     *
     * @param entity 测站与解析组关联关系实体
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2021/12/31
     **/
    @Log(title = "测站与解析组关联关系", businessType = BusinessType.UPDATE)
    @PutMapping("/v1/PUT/st/group")
    public AjaxResult edit(@Validated @RequestBody RelStAnalysisGroupConfigEntity entity) {
        boolean exist = service.checkStGroupExist(entity);
        if (exist) {
            String stCd = entity.getStCd();
            String funcCode = entity.getElementAnalysisGroupConfigEntity().getFuncCode();
            return AjaxResult.error("修改测站与解析组关联关系失败，测站编码：" + stCd + "，功能码:" + funcCode + "已存在。");
        }
        return toAjax(service.update(entity));
    }

    /**
     * 初始化测站缓存数据到redis中，数据格式（stCode, List<groupIdList>），测站编码，测站挂载的解析组编码集合
     */
    @Log(title = "初始化配置数据到redis", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/st/group/cache")
    public AjaxResult initCache() {
        service.initCache();
        return AjaxResult.success();
    }


    /**
     * 删除测站与解析组关联关系
     *
     * @param ids 一个或多个测站与解析组关联关系主键
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2021/12/31
     **/
    @Log(title = "测站与解析组关联关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/v1/DELETE/st/group/{ids}")
    public AjaxResult remove(@PathVariable long[] ids) {
        service.delete(ids);
        return success();
    }
}
